<?php
//$_GET['action']={test|rate|rate_detail|group|check_title|template_list}

//rate:
//$_GET['node_id']
//$_GET['value']={view|hear|..}

//rate_detail:
//$_GET['node_id']

//group:
//$_GET['id1']
//$_GET['id2']
//$_GET['type']

//check_title:
//$_GET['title']
//$_GET['type']

//template:

	include 'include/function.php';

	if($_GET['action']=='test') {
		echo 'Yes, it\'s just a test';
		include 'include/header.php';
		echo '<script type="text/javascript" src="include/ajax.js"></script>';
		echo '<table border=1 class="table_list row_hover">';
		echo '<tr>';
			echo '<th>' . _d('g_title') . '</th>';
			echo '<th>' . 'action' . '</th>';
		echo '</tr>';
		echo '<tr><td colspan=2 id="test_group_add"><a href="group.php?action=add">Add group</a></td></tr>';
		echo '</table>';
		echo '<script>drawGroupList(document.getElementById("test_group_add"),0,0);</script>';
		
	} else if($_GET['action']=='rate') {
		if((!isset($_user_id)) || (!isset($_GET['node_id']))) {
			echo 'failed';
		} else {
			$ajax_node_id=get_to_mysql($_GET['node_id']);
			$ajax_value=get_to_mysql($_GET['value']);
			$query="update user_rate set rate='$ajax_value', visit_time=now() "
				. "where user_id=$_user_id and node_id=$ajax_node_id ";
			$ret=oj_query($query);
			echo 'rate updated';
		}
	} else if($_GET['action']=='rate_detail') {
		if(!isset($_GET['node_id'])) {
			echo 'failed';
		} else {
			$ajax_node_id=get_to_mysql($_GET['node_id']);
			$query="select rate,count(rate) from user_rate where node_id=$ajax_node_id group by rate";
			$ret=oj_query($query);
			while($row=mysql_fetch_row($ret)) {
				$ajax_rate[$row[0]]=$row[1];
			}
			echo '<table border=1 class="table_list row_hover">';
			foreach($rate_array as $key=>$val) {
				if(isset($ajax_rate[$key]))
					echo '<tr><td>' . _d($val) . '</td><td>' . $ajax_rate[$key] . '</td></tr>';
				else
					echo '<tr><td>' . _d($val) . '</td><td>0</td></tr>';
			}
			echo '</table>';
		}
	} else if($_GET['action']=='group') {
		include 'include/grouplist.php';
		if(!isset($_GET['type'])) {
			echo 'failed';
		} else {
			$list=get_group_list(get_to_mysql($_GET['type']),
				(isset($_GET['id1'])?get_to_mysql($_GET['id1']):0),
				(isset($_GET['id2'])?get_to_mysql($_GET['id2']):0));
			$row_is_first=1;
			foreach($list as $row_key=>$row_val) {
				if($row_is_first==0)echo '<hr>';
				$col_is_first=1;
				foreach($row_val as $col_key=>$col_val) {
					if($col_is_first==0)echo '<br>';
					echo $col_val;
					$col_is_first=0;
				}
				$row_is_first=0;
			}
		}
	} else if($_GET['action']=='check_title') {
		$ajax_type=$_GET['type'];
		if($ajax_type!='group'&&$ajax_type!='node') echo 'failed';
		else {
			$query="select title,id from `$ajax_type` where title like \"%{$_GET['title']}%\" limit 6 ";
			$ret=oj_query($query);
			$count=0;
			while($row=mysql_fetch_row($ret)) {
				if($count>0)echo '<br>';
				if($count<5) {
					if($ajax_type=='node')
						echo "<a href='shownode.php?id={$row[1]}' target='_blank'>" . htmlspecialchars($row[0]) . '</a>';
					else
						echo "<a href='group.php?action=show&id={$row[1]}' target='_blank'>" . htmlspecialchars($row[0]) . '</a>';
					$count++;
				} else echo "<a href='search.php?type=$ajax_type&title=" . urlencode($_GET['title']) . '\' target=\'_blank\'>more</a>';
			}
		}
	} else if($_GET['action']=='template_list') {
		$template_id=get_special_page_id('Templates');
		if(empty($template_id))die();
		$query="select title,id from node where parent=$template_id ";
		$ret=oj_query($query);
		$not_first=0;
		while($row=mysql_fetch_row($ret)) {
			if($not_first)echo '<br>';
			echo $row[1] . ':' . htmlspecialchars($row[0]);
			$not_first=1;
		}
	}

?>
